namespace Tools
{
    using System;
    
    public sealed class CRC16Rev
    {
        public static ushort CalculateCRC16(byte[] Data)
        {
            return CalculateCRC16(Data, 0, Data.Length);
        }
        
        public static ushort CalculateCRC16(byte[] Data, int Offset, int Length)
        {
            return CalculateCRC16(Data, Offset, Length, 0xffff, 0x8408);
        }
        
        public static ushort CalculateCRC16(byte[] Data, int Offset, int Length, ushort Preset, ushort Polynom)
        {
            if (Data == null)
            {
                throw new ArgumentNullException("Data", "Data Is Null.");
            }
            if (((Offset < 0) || (Length < 0)) || ((Offset + Length) > Data.Length))
            {
                throw new ArgumentOutOfRangeException();
            }
            ushort num = Preset;
            int num2 = Offset + Length;
            for (int i = Offset; i < num2; i++)
            {
                num = (ushort) (num ^ ((ushort) (Data[i] & 0xff)));
                for (byte j = 0; j < 8; j = (byte) (j + 1))
                {
                    if ((num & 1) > 0)
                    {
                        num = (ushort) ((num >> 1) ^ Polynom);
                    }
                    else
                    {
                        num = (ushort) (num >> 1);
                    }
                }
            }
            return num;
        }
        
        public static bool CheckCRC16(byte[] Data)
        {
            return CheckCRC16(Data, 0, Data.Length);
        }
        
        public static bool CheckCRC16(byte[] Data, int Offset, int Length)
        {
            return CheckCRC16(Data, 0, Data.Length, 0xffff, 0x8408);
        }
        
        public static bool CheckCRC16(byte[] Data, int Offset, int Length, ushort Preset, ushort Polynom)
        {
            return (CalculateCRC16(Data, Offset, Length, Preset, Polynom) == 0);
        }
    }
}
